//
//  LGSeachBar.m
//  微博
//
//  Created by MS on 15-12-4.
//  Copyright (c) 2015年 MS. All rights reserved.
//

#import "LGSeachBar.h"
#import "UIImage+Image.h"
#import "UIView+Frame.h"

#define margin 10
#define Delet_Btn_Margin 25
@interface LGSeachBar ()
@property (nonatomic,weak) UIImageView * imageV;
@property (nonatomic,weak) UIButton * btn;
@property (nonatomic,weak) UIButton * rightBtn;
@end

@implementation LGSeachBar

-(void)setPlaceHolderhidden:(BOOL)placeHolderhidden
{
    _placeHolderhidden = placeHolderhidden;
    _btn.hidden = placeHolderhidden;
    _rightBtn.hidden = !placeHolderhidden;
}

-(void)setPlaceHolder:(NSString *)placeHolder
{
    _placeHolder = placeHolder;
    [self.btn setTitle:placeHolder forState:UIControlStateNormal];
    [_btn sizeToFit];
}

-(void)setImage:(UIImage *)image
{
    _image = image;
    
    UIImage * newImage = [UIImage image:image byScalingToSize:CGSizeMake(self.height*0.6, self.height*0.6)];
    [_btn setImage:newImage forState:UIControlStateNormal];
    [_btn sizeToFit];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    if (self =[super initWithFrame:frame])
    {
      //  self.textAlignment = NSTextAlignmentCenter;
        self.tintColor = [UIColor lightGrayColor];
        self.borderStyle = UITextBorderStyleRoundedRect;
    
        self.backgroundColor = [UIColor colorWithRed:0.92f green:0.92f blue:0.92f alpha:1.00f];
        
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:btn];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
       
        btn.adjustsImageWhenHighlighted = NO;
        btn.titleEdgeInsets  = UIEdgeInsetsMake(0, margin, 0, 0);
        self.btn = btn;
        btn.userInteractionEnabled = NO;
        [btn sizeToFit];
        
        self.font = [UIFont systemFontOfSize:12];
        
        UIButton * deletBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        deletBtn.adjustsImageWhenHighlighted = NO;
        deletBtn.hidden = YES;
        [deletBtn addTarget:self action:@selector(deletBtn) forControlEvents:UIControlEventTouchUpInside];
        
        
        UIImage * newImage = [UIImage image:[UIImage imageNamed:@"input_delete"] byScalingToSize:CGSizeMake(self.height*0.7, self.height*0.7)];
        
        [deletBtn setImage:newImage forState:UIControlStateNormal];
        [deletBtn sizeToFit];
        self.rightBtn = deletBtn;
        self.rightView = deletBtn;
        
        //一定要设置左边视图模式才能正常显示
        self.rightViewMode = UITextFieldViewModeAlways;
    }
    
    return self;
}

-(void)animationPlaceHolderView
{
    if (self.text.length) {
        
        self.btn.center = CGPointMake((self.frame.size.width-self.rightBtn.width)*0.5, self.frame.size.height*0.5);
    }
    
}

-(void)deletBtn
{
    self.text = nil;
    self.placeHolderhidden = NO;
}


- (CGRect)rightViewRectForBounds:(CGRect)bounds
{
    return CGRectMake(self.width-Delet_Btn_Margin, 0, self.height, self.height);
}


-(void)setFont:(UIFont *)font
{
    [super setFont:font];
    
     self.btn.titleLabel.font = font;
     [self.btn sizeToFit];
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    self.btn.width = _btn.width + margin;
    self.btn.center = CGPointMake((self.frame.size.width-self.rightBtn.width)*0.5, self.frame.size.height*0.5);
    
}

@end
